package com.aier.service.report.impl;

import com.aier.bean.foreign.ForeignBase;
import com.aier.bean.report.BuildsPosition;
import com.aier.service.foreign.ForeignBaseService;
import com.aier.service.report.BuildsPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

@Service
public class BuildsPositionServiceImpl implements BuildsPositionService {

    @Autowired
    private ForeignBaseService foreignBaseService;

    @Override
    public List<BuildsPosition> buildBuildsPosition() {
        List<ForeignBase> foreignBaseList = foreignBaseService.getAll();
        List<BuildsPosition> buildsPositionList = new ArrayList<>();
        foreignBaseList.stream().forEach(foreignBase -> {
            BuildsPosition buildsPosition = new BuildsPosition(foreignBase);
            buildsPosition.setSingleBoxVolumetric(
                    foreignBase.getBoxLength()
                            .multiply(foreignBase.getBoxWidth()
                                    .multiply(foreignBase.getBoxHeight()))
                            .divide(BigDecimal.valueOf(1000000))
                            .setScale(2, RoundingMode.HALF_UP)
            );
            buildsPosition.setTotalVolumetric(foreignBase.getBoxLength()
                    .multiply(foreignBase.getBoxWidth()
                            .multiply(foreignBase.getBoxHeight())
                            .multiply(foreignBase.getBoxNum()))
                    .divide(BigDecimal.valueOf(1000000))
                    .setScale(2, RoundingMode.HALF_UP)
            );
            buildsPosition.setTotalWeight(foreignBase.getWeight()
                    .multiply(foreignBase.getBoxNum())
                    .setScale(2, RoundingMode.HALF_UP)
            );
            buildsPositionList.add(buildsPosition);
        });

        return buildsPositionList;
    }
}
